{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [],
   "source": [
    "import urllib.request\n",
    "import urllib.parse"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "200\n",
      "[('Server', 'nginx/1.4.6 (Ubuntu)'), ('Date', 'Fri, 31 Aug 2018 05:19:17 GMT'), ('Content-Type', 'text/html; charset=utf-8'), ('Transfer-Encoding', 'chunked'), ('Connection', 'close'), ('X-Frame-Options', 'SAMEORIGIN')]\n",
      "nginx/1.4.6 (Ubuntu)\n"
     ]
    }
   ],
   "source": [
    "response = urllib.request.urlopen('http://www.xumingguo.cn')\n",
    "print(response.status)\n",
    "print(response.getheaders())\n",
    "print(response.getheader('Server'))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 74,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{\n",
      "  \"args\": {}, \n",
      "  \"data\": \"\", \n",
      "  \"files\": {}, \n",
      "  \"form\": {\n",
      "    \"word\": \"hello\"\n",
      "  }, \n",
      "  \"headers\": {\n",
      "    \"Accept-Encoding\": \"identity\", \n",
      "    \"Connection\": \"close\", \n",
      "    \"Content-Length\": \"10\", \n",
      "    \"Content-Type\": \"application/x-www-form-urlencoded\", \n",
      "    \"Host\": \"httpbin.org\", \n",
      "    \"User-Agent\": \"Python-urllib/3.7\"\n",
      "  }, \n",
      "  \"json\": null, \n",
      "  \"origin\": \"117.136.30.202\", \n",
      "  \"url\": \"http://httpbin.org/post\"\n",
      "}\n",
      "\n"
     ]
    }
   ],
   "source": [
    "data = bytes(urllib.parse.urlencode({'word': 'hello'}), encoding='utf-8')\n",
    "response = urllib.request.urlopen('http://httpbin.org/post', data=data)\n",
    "print(response.read().decode())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 79,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Time Out\n"
     ]
    }
   ],
   "source": [
    "import socket\n",
    "import urllib.request\n",
    "import urllib.error\n",
    "try:\n",
    "    response = urllib.request.urlopen('http://httpbin.org/get', timeout=0.1)\n",
    "except urllib.error.URLError as e:\n",
    "    if isinstance(e.reason, socket.timeout):\n",
    "        print('Time Out')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.0"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
